package com.sty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sty.enums.PaymentStatus;
import com.sty.enums.YesOrNo;
import com.sty.mapper.pay.OrderMapper;
import com.sty.org.n3r.idworker.Sid;
import com.sty.pojo.Orders;
import com.sty.pojo.bo.MerchantOrdersBO;
import com.sty.service.PaymentOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class PaymentOrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements PaymentOrderService {
	@Autowired
	private Sid sid;

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public boolean createPaymentOrder(MerchantOrdersBO merchantOrdersBO) {

		String id = sid.nextShort();

		Orders paymentOrder = new Orders();
		BeanUtils.copyProperties(merchantOrdersBO, paymentOrder);

		paymentOrder.setId(id);
		paymentOrder.setPayStatus(PaymentStatus.WAIT_PAY.type);
		paymentOrder.setComeFrom("天天吃货");
		paymentOrder.setIsDelete(YesOrNo.NO.type);
		paymentOrder.setCreatedTime(new Date());

		int result = getBaseMapper().insert(paymentOrder);
		return result == 1;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public Orders queryOrderByStatus(String merchantUserId, String merchantOrderId, Integer orderStatus) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(Orders::getMerchantOrderId, merchantOrderId)
				.eq(Orders::getMerchantUserId, merchantUserId)
				.eq(Orders::getPayStatus, orderStatus);

		return getBaseMapper().selectOne(queryWrapper);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public String updateOrderPaid(String merchantOrderId, Integer paidAmount) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(Orders::getMerchantOrderId, merchantOrderId);

		Orders paidOrder = new Orders();
		paidOrder.setPayStatus(PaymentStatus.PAID.type);
		paidOrder.setAmount(paidAmount);

		getBaseMapper().update(paidOrder, queryWrapper);

		return queryMerchantReturnUrl(merchantOrderId);
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	String queryMerchantReturnUrl(String merchantOrderId) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(Orders::getMerchantOrderId, merchantOrderId);
		Orders order = getBaseMapper().selectOne(queryWrapper);
		return order.getReturnUrl();
	}

	@Override
	public Orders queryOrderInfo(String merchantUserId, String merchantOrderId) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(Orders::getMerchantOrderId, merchantOrderId)
				.eq(Orders::getMerchantUserId, merchantUserId);

		return getBaseMapper().selectOne(queryWrapper);
	}
}
